let winLocalStorage = window.localStorage;

let cache;

class UserData {
	constructor() {
		cache = {};
		let data = winLocalStorage.getItem('userData');
		if (data) {
			try {
				cache = JSON.parse(data);
			} catch (error) {
				cache = {};
			}
		}
	}
	get() {
		return cache;
	}

	set(object) {
		cache = object;
		winLocalStorage.setItem('userData', JSON.stringify(cache));
	}

	setKey(key, value) {
		cache[key] = value;
		winLocalStorage.setItem('userData', JSON.stringify(cache));
	}

	hasPower(id) {
		let power = cache.power || [];
		if (power.indexOf(id) !== -1) {
			return true;
		} else {
			return false;
		}
	}

	remove() {
		cache = {};
		winLocalStorage.removeItem('userData');
	}

	clearToken() {
		delete cache.token;
		winLocalStorage.setItem('userData', JSON.stringify(cache));
	}
}

if (!window.userData) {
	window.userData = new UserData();
}
export default window.userData;
